package window.childTab;

import javax.swing.*;
import java.awt.*;

public class targetTab {
    // Main Panel
    private JPanel jPanel;
    // Split Panel
    private JSplitPane jSplitPane =new JSplitPane();
    // Left Panel
    private JPanel left_panel = new JPanel();
    // Right Panel
    private JPanel right_panel =new JPanel();

    // Left Text Panel
    private JTextPane leftTextPanel = new JTextPane();
    // Left Scroll Panel
    private JScrollPane leftScrollPanel = new JScrollPane();
    // Left Title
    private JLabel leftTitle = new JLabel("<html> Request<br><br></html>");

    // Right Behind Panel
    private JPanel rightBehindPanel = new JPanel();
    // Right Injection Panel
    JPanel rightInjectionPanel = new JPanel();
    // Right Injection Text Panel
    private JPanel rightTextPanel = new JPanel();
    // Right Title
    private JLabel rightTitle = new JLabel("<html>  Injection Point<br><br></html>");
    // Right Generate Button
    private JButton generateButton = new JButton("GENERATE");
    // Right ATTACK Button
    private JButton attackButton = new JButton("  start attack  ");

    public targetTab() {
        this.jPanel = new JPanel();
        this.jPanel.setLayout(new BorderLayout());
    }

    public JPanel getjPanel() {
        return jPanel;
    }

    public void setjPanel(JPanel jPanel) {
        this.jPanel = jPanel;
    }

    public void setLeftTextPanel(String s) {
        leftTextPanel.setText(s);
    }

    /**
     * 将 Request 参数加入滚动列表中
     * @param panel
     * @param list
     * @param type
     * @return
     */
    public JPanel addTable(JPanel panel, String[] list, String type) {
        JPanel partPanel = new JPanel();
        JPanel paramPanel = new JPanel();
        JPanel typePanel = new JPanel();
        JScrollPane paramScrollPanel = new JScrollPane();

        partPanel.setLayout(new BorderLayout());
        paramPanel.setLayout(new BoxLayout(paramPanel, BoxLayout.Y_AXIS));
        typePanel.setLayout(new BorderLayout());

        JCheckBox tempCheckBox = new JCheckBox(type);
        typePanel.add(tempCheckBox, BorderLayout.NORTH);

        for(int i=0; i<list.length; i++) {
            paramPanel.add(new JCheckBox(list[i]));
        }
        paramScrollPanel.setViewportView(paramPanel);

        partPanel.add(typePanel, BorderLayout.WEST);
        partPanel.add(paramScrollPanel, BorderLayout.CENTER);

        panel.add(partPanel);

        return panel;
    }

    /**
     * 将空白标签加入面板进行分隔（1 line）
     * @param panel
     * @return
     */
    public JPanel addEmptyLabel(JPanel panel, String place) {
        JLabel emptyLabel = new JLabel("<html><br></html>");
        if(place.equals("")) {
            panel.add(emptyLabel);
        }
        else {
            panel.add(emptyLabel, place);
        }
        return panel;
    }

    /**
     * 设置targetTab的面板布局
     */
    public void setLayout(String request) {
        // 【测试】
        String[] test = new String[]{"sdfjsd","sdfd","kok","sfadfadsfa","fdsfasfkdsfks","psldfapsefksef","sdfsffdsbg", "pslpds","o", "sfvgfdmskzdmfk"};

        // Left Setting
        leftTitle.setFont(new Font("宋体", 1, 17));
        leftTitle.setForeground(new Color(255,102,51));

        leftTextPanel.setText(request);
        leftTextPanel.setFont(new Font("宋体",0,16));

        leftScrollPanel.setViewportView(leftTextPanel);

        left_panel.setLayout(new BorderLayout());
        left_panel.add(leftTitle, BorderLayout.PAGE_START);
        left_panel.add(leftScrollPanel, BorderLayout.CENTER);

        // Right Setting
        rightTitle.setFont(new Font("宋体", 1, 17));
        rightTitle.setForeground(new Color(255,102,51));

        attackButton.setFont(new Font("Arial",1,17));
        attackButton.setMargin(new Insets(0,0,0,0));
        attackButton.setBorderPainted(false);
        attackButton.setFocusPainted(false);
        attackButton.setForeground(new Color(255,255,255));
        attackButton.setBackground(new Color(255,102,51));

        rightTextPanel.setLayout(new BoxLayout(rightTextPanel, BoxLayout.Y_AXIS));
        rightTextPanel = addTable(rightTextPanel, test, "GET          ");
        rightTextPanel = addEmptyLabel(rightTextPanel, "");
        rightTextPanel = addTable(rightTextPanel, test, "POST       ");
        rightTextPanel = addEmptyLabel(rightTextPanel, "");
        rightTextPanel = addTable(rightTextPanel, test, "HEADER  ");

        rightInjectionPanel.setLayout(new BorderLayout());
        rightInjectionPanel = addEmptyLabel(rightInjectionPanel, "First");
        rightInjectionPanel.add(rightTextPanel, BorderLayout.CENTER);
        rightInjectionPanel = addEmptyLabel(rightInjectionPanel, "South");

        rightBehindPanel.setLayout(new BorderLayout());
        rightBehindPanel.add(generateButton, BorderLayout.NORTH);
        rightBehindPanel.add(rightInjectionPanel, BorderLayout.CENTER);
        rightBehindPanel.add(attackButton, BorderLayout.SOUTH);

        right_panel.setLayout(new BorderLayout());
        right_panel.add(rightTitle, BorderLayout.NORTH);
        right_panel.add(rightBehindPanel, BorderLayout.CENTER);

        // jSplit Setting
        jSplitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
        jSplitPane.setDividerSize(5);
        jSplitPane.setLeftComponent(left_panel);
        jSplitPane.setRightComponent(right_panel);
        jSplitPane.setResizeWeight(.5d);

        // jPanel Setting
        jPanel.add(jSplitPane);
    }
}
